package com.rparoxysm.tribase.data.types;

import com.rparoxysm.util.Debug;

import java.text.AttributedCharacterIterator;
import java.text.CharacterIterator;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class WeatherCondition {
    private static final String CLASS_NAME = "WeatherCondition";
    private static final int MIN_TEMP = -500;
    private static Debug debug = new Debug(CLASS_NAME);
    private static String UNITS = "Imperial";

    private Calendar time;
    private String weather;
    private int temperature;
    private int humidity;
    private String windDirection;
    private int windDirectionDegrees;
    private double windSpeed;
    private int windGust;
    private double pressure;
    private int dewpoint;
    private int heatIndex;
    private int windchill;
    private double visibility;
    private String iconName;
    private String station;

    public WeatherCondition() {
	time                 = null;
	weather              = "";
	temperature          = -1000;
	humidity             = -1;
	windDirection        = "";
	windDirectionDegrees = -1;
	windSpeed            = -1;
	windGust             = -1;
	pressure             = -1.0;
	dewpoint             = -1000;
	heatIndex            = -1000;
	windchill            = -1000;
	visibility           = -1.0;
	iconName             = "";
	station              = null;
    }

    public String   getWeather()              { return weather; }
    public int      getTemperature()          { return temperature; }
    public int      getHumidity()             { return humidity; }
    public String   getWindDirection()        { return windDirection; }
    public int      getWindDirectionDegrees() { return windDirectionDegrees; }
    public double   getWindSpeed()            { return windSpeed; }
    public int      getWindGust()             { return windGust; }
    public double   getPressure()             { return pressure; }
    public int      getDewpoint()             { return dewpoint; }
    public int      getHeatIndex()            { return heatIndex; }
    public int      getWindchill()            { return windchill; }
    public double   getVisibility()           { return visibility; }
    public String   getIconName()             { return iconName; }
    public Calendar getTime()                 { return time; }
    public String   getStation()              { return station; }

    public void setWeather(String w)           { weather = w; }
    public void setTemperature(int t)          { temperature = t; }
    public void setHumidity(int h)             { humidity = h; }
    public void setWindDirection(String d)     { windDirection = d; }
    public void setWindDirectionDegrees(int d) { windDirectionDegrees = d; }
    public void setWindSpeed(double s)         { windSpeed = s; }
    public void setWindGust(int s)             { windGust = s; }
    public void setPressure(double p)          { pressure = p; }
    public void setDewpoint(int t)             { dewpoint = t; }
    public void setHeatIndex(int t)            { heatIndex = t; }
    public void setWindchill(int t)            { windchill = t; }
    public void setVisibility(double v)        { visibility = v; }
    public void setIconName(String n)          { iconName = n; }
    public void setTime(Calendar dtg)          { time = dtg; }
    public void setStation(String where)       { station = where; }
    
    public String toString() {
	String ret = new String("Weather Condition;\n");
	ret += "    Time: " + formatDate(getTime()) + "\n";
	ret += "    Weather: " + getWeather() + "\n";
	ret += "    Temperature: " + (temperature >= MIN_TEMP ? getTemperature() : "NA") + "\n";
	ret += "    Humidity: " + (humidity >= 0 ? getHumidity() : "NA") + "\n";
	ret += "    Wind Direction: " + getWindDirection() + "\n";
	ret += "    Wind Degrees: " + (windDirectionDegrees >= 0 ? getWindDirectionDegrees() : "NA") + "\n";
	ret += "    Wind Speed: " + (windSpeed >= 0 ? getWindSpeed() : "NA") + "\n";
	ret += "    Wind Gust: " + (windGust >= 0 ? getWindGust() : "NA") + "\n";
	ret += "    Air Pressure: " + (pressure >= 0 ? getPressure() : "NA") + "\n";
	ret += "    Dewpoint: " + (dewpoint >= MIN_TEMP ? getDewpoint() : "NA") + "\n";
	ret += "    Heat Index: " + (heatIndex >= MIN_TEMP ? getHeatIndex() : "NA") + "\n";
	ret += "    Windchill: " + (windchill >= MIN_TEMP ? getWindchill() : "NA") + "\n";
	ret += "    Visibility: " + (visibility >= 0 ? getVisibility() : "NA") + "\n";
	ret += "    Icon Name: " + getIconName() + "\n";
	ret += "    Station: " + getStation() + "\n";

	return ret;
    }
    
    public String toXml() {
	String ret = new String();
	ret += "<current_observation>\n";	
	ret += "   <observation_time_rfc822>" + formatDate(getTime()) + "</observation_time_rfc822>\n";
	ret += "   <weather>"+ getWeather() + "</weather>\n";
	ret += "   <temp_f>" + getTemperature() + "</temp_f>\n";
	ret += "   <relative_humidity>" + (humidity >= 0 ? getHumidity() : "NA") + "</relative_humidity>\n";
	ret += "   <wind_dir>" + getWindDirection() + "</wind_dir>\n";
	ret += "   <wind_degrees>" + (windDirectionDegrees >= 0 ? getWindDirectionDegrees() : "NA") + "</wind_degrees>\n";
	ret += "   <wind_mph>" + (windSpeed >= 0 ? getWindSpeed() : "NA") + "</wind_mph>\n";
	ret += "   <wind_gust_mph>" + (windGust >= 0 ? getWindGust() : "NA") + "</wind_gust_mph>\n";
	ret += "   <pressure_in>" + (pressure >= 0 ? getPressure() : "NA") + "</pressure_in>\n";
	ret += "   <dewpoint_f>" + (dewpoint >= MIN_TEMP ? getDewpoint() : "NA") + "</dewpoint_f>\n";
	ret += "   <heat_index_f>" + (heatIndex >= MIN_TEMP ? getHeatIndex() : "NA") + "</heat_index_f>\n";
	ret += "   <windchill_f>" + (windchill >= MIN_TEMP ? getWindchill() : "NA") + "</windchill_f>\n";
	ret += "   <visibility_mi>" + (visibility >= 0 ? getVisibility() : "NA") + "</visibility_mi>\n";
	ret += "   <icon_url_name>" + getIconName() + "</icon_url_name>\n";
	ret += "   <station_id>" + getStation() + "</station_id>\n";
	ret += "</current_observation>\n";

	return ret;
    }

    public static String formatDate(Calendar dtg) {
	StringBuffer buffer = new StringBuffer();
	SimpleDateFormat sdf = 
	    new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
	
	AttributedCharacterIterator aci = 
	    sdf.formatToCharacterIterator(dtg.getTime());
	String dtgString = new String();
	for(char c = aci.first(); c != CharacterIterator.DONE; c = aci.next()) {
	    dtgString += c;
	}
	
	return dtgString;
    }
}
